

**************************************************************************************************
********* THIS FILE JUST CREATES A SUM STATS TABLE FOR THE I2I DATA TO SHOW THE RELATIONSHIPS ****
********* BETWEEN BENEFICIARY STATUS AND GEOGRAPHIC COVARIATES 
*** DATE: 5/6/20 *********
*** AUTHOR: JAG **************
*** VERSION: STATA 16 ********

clear*

cd "C:\Users\03638881\Dropbox\Satellite data measurement\"
glo data "C:\Users\03638881\Dropbox\Satellite data measurement\Data\"
glo outputs "C:\Users\03638881\Dropbox\Satellite data measurement\Latex\tables\"

use "${data}PESi2iPanelDataShortAugust2021.dta", clear
bysort APEJNCNID: egen everben = max(beneL1)
bysort APEJNCNID: egen meanscenesL5 = mean(path_row_counts_2000_2015_L5)
la var meanscenesL5 "Mean cloud-free L5 scenes, 2000-2015"
bysort APEJNCNID: egen meanscenesL7 = mean(path_row_counts_2000_2015_L7)
la var meanscenesL7 "Mean cloud-free L7 scenes, 2000-2015"

keep if inejido == 1

global vars binary_defor everbene elev_met_mean slope_deg_mean dist_any_road_mt dist_5000_city_km TPApAreaHa pctTC2000 per_maj_ind path_row_counts_2000_2015_L7


qui {
	sort APEJNCNID muni_cve yr
	xtset muni_cve

	*define vars to be used
	loc y "binary_defor"

	xi i.yr
	loc x0 "_Iyr_2002 _Iyr_2003 _Iyr_2004 _Iyr_2005 _Iyr_2006 _Iyr_2007 _Iyr_2008 _Iyr_2009 _Iyr_2010 _Iyr_2011 _Iyr_2012 _Iyr_2013 _Iyr_2014"
	loc x1 "beneL1 everbene elev_met_mean slope_deg_mean dist_any_road_mt dist_5000_city_km TPApAreaHa pctTC2000 per_maj_ind path_row_counts_2000_2015_L7"

	xtreg `y' `x0' `x1', fe i(muni_cve) vce(cl muni_cve)
	keep if e(sample)

}


***** I WANT AREA-WEIGHTED AVERAGES HERE, SO I AM GOING TO USE FREQUENCY WEIGHTS OF THE AREA OF THE POLYGONS

replace TPApAreaHa = round( TPApAreaHa) 


estpost sum $vars if beneL1 == 0
estimates store A
estpost sum  $vars if beneL1 == 1
estimates store B
* Normalized difference: need to fix this to weight normalized difference
preserve 
foreach var in  $vars {
sum `var' if beneL1 == 0
scalar rmu1 = r(mean)
scalar rsd1 = r(sd)    
sum `var' if beneL1 == 1
scalar rmu2 = r(mean)
scalar rsd2 = r(sd)    
replace `var' = (rmu2 - rmu1)/(sqrt(rsd2^2 + rsd1^2))
}

estpost sum $vars
estimates store C
restore


esttab A B C using "${outputs}sumstats_application.tex", replace f cells("mean(fmt(3))") label style (tex) ///
collabels(none) mtitle( "Non-beneficiary land" "Beneficiary land" "Norm diff")  
